<?php

namespace app\controllers;

use app\models\Area;
use app\tools\Re;
use Yii;

class AreaController extends BaseController
{
    //省市区一起返回
    public function actionList(){
        $data = [];
        $province_list = Area::find()->where(['parent_id' => 0])->select("id,name")->all();
        foreach ($province_list as $p) {
          $city_list = Area::find()->where(['level' => 1, 'parent_id' => $p->id])->select("id,name")->all();
          $city_data = [];
          foreach ($city_list as $c) {
            $area_list = Area::find()->where(['level' => 2, 'parent_id' => $c->id])->andWhere("name <> '市辖区'")->select("id,name")->all();
            $city_data[] = [
              'id' => $c->id,
              'name' => $c->name,
              'area_list' => $area_list
            ];
          }
          $data[] = [
            'id' => $p->id,
            'name' => $p->name,
            'city_list' => $city_data
          ];
        }
        return Re::data(['data' => $data]);
    }
}
